﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using LowLevelGraphics.ColorSpaces;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Enhance channels
    /// </summary>
    public class EnhanceChannels : BaseImageFilter
    {
        PerPixelFilter m_PerPixelFilter = null;

        protected Type m_AbstractColorSpace = typeof(sRGB);
        protected object[] m_aValue = new object[] { 1.0f, 1.0f, 1.0f };

        public EnhanceChannels()
        {
            m_PerPixelFilter = new PerPixelFilter(
                new PerPixelFilter.PerPixelFilterDelegate(
                    delegate(Color _color)
                    {
                        AbstractColorSpace abstractColorSpace = (AbstractColorSpace)Activator.CreateInstance(m_AbstractColorSpace,new object[]{_color});
                        object[] aValue = abstractColorSpace.GetValues();
                        for (int i = 0; i < m_aValue.Length; i++)
                        {
                            bool bTypesOk = (aValue[i].GetType() == m_aValue[i].GetType());
                            if (bTypesOk)
                            {
                                if (aValue[i].GetType() == typeof(double))
                                {
                                    double dValue = (double)aValue[i] + (double)m_aValue[i];
                                    aValue[i] = dValue;
                                }
                                else if (aValue[i].GetType() == typeof(int))
                                {
                                    int nValue = (int)aValue[i] + (int)m_aValue[i];
                                    aValue[i] = nValue;
                                }
                                else if (aValue[i].GetType() == typeof(float))
                                {
                                    float fValue = (float)aValue[i] + (float)m_aValue[i];
                                    aValue[i] = fValue;
                                }
                            }
                        }
                        abstractColorSpace.SetValues(aValue);
                        return abstractColorSpace.ToColor();
                    }
                    ));
        }

        /// <summary>
        /// used to clone this instance
        /// </summary>
        /// <param name="_enhanceChannels9"></param>
        internal EnhanceChannels(EnhanceChannels _enhanceChannels)
        {
        }

        /// <summary>
        /// Color Space
        /// </summary>
        public Type ColorSpace
        {
            get { return m_AbstractColorSpace; }
            set { m_AbstractColorSpace = value; }
        }

        /// <summary>
        /// Gets or sets values
        /// </summary>
        public object[] Values
        {
            get { return m_aValue; }
            set { m_aValue = null; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap  Execute(UnsafeBitmap _bitmap)
        {
 	        return m_PerPixelFilter.Execute(_bitmap);
        }

        /// <summary>
        /// Clones this instance
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new EnhanceChannels(this);
        }
    }
}
